#include "Main.h"

uint8_t UART_RX,UART_TX;
uint8_t Cnt=0;
unsigned long millis_time;
unsigned long time_ms;
unsigned long first_time;
unsigned long second_time;
bool igniter_state = 0;  //点火棒状态显示
bool motor_state = 0;  //马达状态显示
bool fan_state = 0;  //风扇状态显示
bool feed_state = 0;  //进料状态显示
uint16_t Temperature_Set = 180;  //温度设置
uint8_t time_hour;
uint8_t time_minute;
bool first_temp_set = 0; //上电初次设置温度
uint8_t Project_state = 0;
uint32_t Tuya_Timeout = 0;

#define COMP			PORT_B_14 

void BSTIM0_Handler()
{
	//GPIOC->DO ^= 0x40;
	
	BSTIM0->SR = BSTIM_SR_UIF_Msk;
}

void BSTIM1_Handler()
{
	//GPIOC->DO ^= 0x80;
	
	BSTIM1->SR = BSTIM_SR_UIF_Msk;
}

void WTIM_Handler()
{
	//GPIOC->DO ^= 0x100;
	Cnt++;
	WTIM->INTIF = WTIM_INTIF_CIF_Msk;
}

void UART0_Handler()
{
	if(UART0->SR_b.RXBF == 1)		//receive buffer full interrupt
	{
		UART0->SR = UART_SR_RXBF_Msk;	//clear receive full interrupt flag
		UART_RX = UART0->RDR_b.RXBUF;
		if(UART_RX == 0X55)
		{
			UART0->TDR_b.TXBUF = 0XAA;
		}
	}
	
	if(UART0->SR_b.TXSE == 1)		//transit shift empty interrupt
	{
		//UART0->TDR_b.TXBUF = 0XAA;
		
		UART0->SR = UART_SR_TXSE_Msk;	//clear tx empty interrupt flag
	}
}

void UART1_Handler()
{
	if(UART1->SR_b.RXBF == 1)		//receive buffer full interrupt
	{
		UART1->SR = UART_SR_RXBF_Msk;	//clear receive full interrupt flag
		UART_RX = UART1->RDR_b.RXBUF;
		if(UART_RX == 0X55)
		{
			UART1->TDR_b.TXBUF = 0XAA;
		}
	}
	
	if(UART1->SR_b.TXSE == 1)		//transit shift empty interrupt
	{
		UART1->SR = UART_SR_TXSE_Msk;	//clear tx empty interrupt flag
	}
}

//void UART2_Handler()
//{
//	if(UART2->SR_b.RXBF == 1)					//receive buffer full interrupt
//	{
//		UART2->SR = UART_SR_RXBF_Msk;	//clear receive full interrupt flag
//		UART_RX = UART2->RDR_b.RXBUF;
//		if(UART_RX == 0X55)
//		{
//			UART2->TDR_b.TXBUF = 0XAA;
//		}
//	}
//	
//	if(UART2->SR_b.TXSE == 1)					//transit shift empty interrupt
//	{
//		UART2->SR = UART_SR_TXSE_Msk;	//clear tx empty interrupt flag
//	}
//}

void encode(void)
{
	if(Encoder_TurnL)
	{
		Encoder_TurnL = 0;
		if(Temperature_Set_OnOff&&Temperature_Set_display>180)
		{
			Temperature_Set_display-=5;
		}
		if(time_hour_OnOff&&time_hour_display>0)
		{
			time_hour_display-=1;
		}
		if(time_minute_OnOff&&time_minute_display>0)
		{
			time_minute_display-=1;
		}
	}
	
	if(Encoder_TurnR)
	{
		Encoder_TurnR = 0;
		if(Temperature_Set_OnOff&&Temperature_Set_display<500)
		{
			Temperature_Set_display+=5;
		}
		if(time_hour_OnOff&&time_hour_display<23)
		{
			time_hour_display+=1;
		}
		if(time_minute_OnOff&&time_minute_display<59)
		{
			time_minute_display+=1;
		}
	}
}

void time_updata(void)  //计时更新
{
	time_minute = time_ms%3600000/1000/60;
	time_hour = time_ms/1000/60/60;
}

void APP_config(void)
{
	AiP1632C_init();
	Color_led_int();
	EncoderInit();
	FanCtrl_IOConfig();
	ADInit();
	FanCtrlInit();
	logo_state = 1;  //logo状态显示
	hengang_state = 1;  //横杠状态显示
	uart2_setup();
	wifi_protocol_init();
}



int main(void)
{	
//	MR_WDT_INIT(WDT_4S);			//set watchdog timer to 4s period
	SVD_Init(24-REF_SVD_VOLT);	//set 2.4v low voltage reset
	MR88FX02_SysClk_Init(CoreClk_HSRC48M,0,SYSCLOCK);	//set system clock is internal RC 16Mhz
	MR88FX02_Port_Init(Port_Table);
	SysTick_Config(SYSCLOCK/500); //1ms:1000;  2ms:500; 500us: 2000;
	
	

	APP_config();
	AiP1632C_write_dat(0xff,0);
	Wait_ms(3000);
	AiP1632C_write_dat(0x00,0);
	Wait_ms(1000);
	while(1)
	{
		EncoderMain();
		Key_state_get();
		time_updata();
		ALL_display(Project_state);
		
		if(Onoff_key_flag)
		{
			Onoff_key_flag = 0;
			if(Onoff_state==0)
			{
				Onoff_state = 1;
			}
		}
		else if(Onoff_key_long_flag)
		{
			Onoff_key_long_flag = 0;
			if(Onoff_state==1)
			{
				Onoff_state = 0;
			}
		}
		
		switch(Project_state)
		{
			case INIT_STATE:
				time_icon = 0;
				if(Onoff_state)
				{
					Project_state = RUN_STATE;
				}
				break;
			case OFF_STATE:
				FanCtrlStop();
				if(time_ms==0)
				{
					Project_state = INIT_STATE;
				}
				
				if(Onoff_state)
				{
					time_ms = 0;
					Project_state = RUN_STATE;
				}
				break;
			case RUN_STATE:
				menu_state_updata();  //菜单键下的操作
				TempADC_Main();
				tuya_date_TX();
			  wifi_uart_service();
				if(Encoder_LongKey)
				{
					Encoder_LongKey = 0;
					mcu_reset_wifi();																			//ÖØÖÃwifiÖ¸Áî
					mcu_set_wifi_mode(SMART_CONFIG);	
				}
				if(Onoff_state)
				{
					if(first_temp_set==0)
					{
						menu_num = 1;
					}
					if(first_temp_set == 1)
					{
						menu_state_updata();
						FanCtrlStart();
						fire_state = 1;
					}
				}
				else
				{
					time_minute = 10;
					time_hour = 0;
					time_ms = time_hour*60*60*1000+time_minute*60*1000;
					fire_state = 0;
					Project_state = OFF_STATE;
				}
				if(RTD[RTD_Seq_ProbePG].status)
				{
					Project_state = ERO_STATE;
				}
				
				
				
				
				
				if(touch_get()==2)
				{
				first_time = millis();
				if(first_time - second_time<100)
				{
					Red_ON();
					Blue_OFF();
					Yellow_OFF();
					Orange_OFF();
				}
				else if(first_time - second_time>=100&&first_time - second_time<200)
				{
					Red_OFF();
					Blue_ON();
					Yellow_OFF();
					Orange_OFF();
				}
				else if(first_time - second_time>=200&&first_time - second_time<300)
				{
					Red_OFF();
					Blue_OFF();
					Yellow_ON();
					Orange_OFF();
				}
				else if(first_time - second_time>=300)
				{
					second_time = first_time;
				}
			}
			else
			{
				Red_OFF();
				Blue_OFF();
				Yellow_OFF();
				Orange_OFF();
			}
				
				
				break;
			case ERO_STATE:
				if(!RTD[RTD_Seq_ProbePG].status)
				{
					Project_state = ERO_STATE;
				}
				IgniterClose;
				HeaterFanClose;
				ExhaustFanClose;
				break;
			
			default:   
				break;
		}

		HeatingFanCtrl();
		ExhaustFanCtrl();
		IgniterCtrl();
		
		
		

//		MR_Wdg_Feed();
	}
}

//Tick 1ms
void SysTick_Handler(void)
{
	EncoderClock();
	ADC_Clock_2ms();
	FanCtrlClock();
	millis_time ++;
	Tuya_Timeout ++;
	if(time_ms>1)
	{
		time_ms-=2;
	}
}

unsigned long millis(void)
{
	return millis_time;
	
	
}



